package banks;

import itsbank.*;
import accounts.*;
import database.DatabaseManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Random;

/**
 * the bank, it manages all customers and accounts of its clients
 * @author Mike
 */

public class Bank {
    private int bankID;
    private Hashtable<Integer,Account> accounts;
    private Hashtable<String,Customer> customers;
    private String name;
    private Random rand;

    //creating of a bank is only allowed in this package
    Bank (int id, String _name) throws SQLException {
        bankID = id;
        name = _name;
        rand = new Random();
        accounts = new Hashtable<Integer,Account>();
        ResultSet rs = DatabaseManager.getInstance().query("SELECT * FROM Account WHERE BankID = "+id);
        while (rs.next()) {
            int num = rs.getInt("AccountNumber");
            accounts.put(num, new CheckingAccount(id,num,rs.getInt("OwnerID"),rs.getInt("Balance"),rs.getInt("Limit")));
        }
        customers = new Hashtable<String,Customer>();
    }
    /**
     * gets the id of the bank
     * @return the id
     */
    public int getBankId () {
        return bankID;
    }
    /**
     * gets the name of the bank
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * searches by account no for a account in the database
     * @param id the account number
     * @return the account that was found
     */
    public Account getAccount (int id) {
        return accounts.get(id);
    }
    /**
     * searches by name for a customer in the database
     * @param _name the name of the customer to be searched
     * @return the customer that was found
     */
    public Customer getCustomer (String _name) {
        return customers.get(name);
    }
    /**
     * adds a customer to the client db of the bank
     * @param customer the customer to be added
     */
    public void addCustomerToClientDB(Customer customer) {
        if (!customers.contains(customer.name)) {
            customers.put(customer.name, customer);
        }
    }
    /**
     * removes a customer from the client db of the bank
     * @param customer the customer to be removed
     */
    public void removeCustomerFromClientDB(Customer customer) {
        if (customers.contains(customer.name)) {
            customers.remove(customer.name);
        }
    }
    /**
     * will open a new account for a given customer
     * @param type the type of the account
     * @param customer the customer the account belongs to
     */
    public String openAccount(AType type, Customer customer) throws SQLException {
        int AccNumber;
        do {
           AccNumber  = rand.nextInt(1000000);
           try {
                ResultSet rs = DatabaseManager.getInstance().query("SELECT * FROM account WHERE AccountNumber = "+AccNumber);
                if (rs.first()) {
                    continue;
                }
           }
           catch (SQLException e) {
               continue;
           }
        } while(false);

        DatabaseManager.getInstance().update("INSERT INTO account VALUES("+AccNumber+","+bankID+","+customer.ID+",0,200)");

        addCustomerToClientDB(customer);
        if (type == AType.Checking) {
            CheckingAccount acc = new CheckingAccount(bankID,customer.ID,AccNumber,0,200);
            accounts.put(acc.getAccountNo(),acc);
           customer.getAccounts().add(acc);
        }
        else {
            //other accounts are not supported yet
            throw new IllegalArgumentException();
        }
        return this.getName();
    }
}